package com.cyz.user.userService.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyz.common.bean.Card;
import com.cyz.common.bean.UserCard;
import com.cyz.common.dto.CardDto;
import com.cyz.common.enums.MyEnums;
import com.cyz.common.mapper.CardMapper;
import com.cyz.common.utils.BeanCopyUtil;
import com.cyz.common.utils.MyRedisUtil;
import com.cyz.common.utils.R;
import com.cyz.common.utils.SecurityUtils;
import com.cyz.user.userService.CardButtonService;
import com.cyz.user.userService.CardService;
import com.cyz.user.userService.UserCardService;
import com.cyz.user.vo.AdminCardVo;
import com.cyz.user.vo.CardVo;
import com.cyz.user.vo.UserCardVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 34928
 * @description 针对表【card】的数据库操作Service实现
 * @createDate 2023-02-21 08:14:10
 */
@Service
public class CardServiceImpl extends ServiceImpl<CardMapper, Card>
        implements CardService {

    @Autowired
    private CardButtonService cardButtonService;
//    @Autowired
//    private RedisUtil redisUtil;
    @Autowired
    private MyRedisUtil myRedisUtil;
    @Autowired
    private CardService cardService;
    @Autowired
    private UserCardService userCardService;

    @Override
    public R queryCards() {
        //创建筛选器
        LambdaQueryWrapper<Card> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据更新时间排序
        lambdaQueryWrapper.orderByDesc(Card::getUpdateTime);
        //查询相关课程
        List<Card> list = list(lambdaQueryWrapper);
        //vo处理
        List<UserCardVo> adminCardVos = BeanCopyUtil.copyBeanList(list, UserCardVo.class);
        //返回数据
        return R.success(adminCardVos);
    }

    @Override
    public R queryCardsAndButtons() {
        //创建筛选器
        LambdaQueryWrapper<Card> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据更新时间排序
        lambdaQueryWrapper.orderByDesc(Card::getUpdateTime);
        //查询相关课程
        List<Card> list = list(lambdaQueryWrapper);
        //vo处理
        List<AdminCardVo> adminCardVos = BeanCopyUtil.copyBeanList(list, AdminCardVo.class);
        adminCardVos.forEach(item->{
            item.setButtonNames(cardButtonService.getbuttonNamesByCardId(item.getId()));
//            item.setButtonIds(cardButtonService.getbuttonIdsByCardId(item.getId()));
        });
        //返回数据
        return R.success(adminCardVos);
    }

    @Override
    public R getCardById(Integer id) {
        Card card = getById(id);
        //从redis中获取viewCount
//        Integer viewCount = Integer.parseInt(redisUtil.getCacheMapValue("card:viewCount", String.valueOf(id)));
        Integer viewCount = (Integer) myRedisUtil.myHashGet("card:viewCount", String.valueOf(id));
        card.setViewCount(viewCount);

        CardVo cardVo = BeanCopyUtil.copyBean(card, CardVo.class);
        return R.success(cardVo);
    }

    @Override
    public R updateViewCount(Integer id) {
        //更新redis中对应 id的浏览量
//        redisUtil.incrementCacheMapValue("card:viewCount","1",1);
        myRedisUtil.myHashIncrement("card:viewCount", String.valueOf(id),1);
        return R.success();
    }

    @Override
    public R queryHotCards() {
        //查找数据
        LambdaQueryWrapper<Card> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //已发布
        lambdaQueryWrapper.eq(Card::getStatus, MyEnums.CARD_RELEASE);
        //根据置顶排序
        lambdaQueryWrapper.orderByDesc(Card::getViewCount);
        //查询相关课程
        List<Card> list = cardService.list(lambdaQueryWrapper);
        List<Card> collect = list.stream()
                .limit(2)
                .collect(Collectors.toList());
        //vo处理
        List<CardVo> cardVos = BeanCopyUtil.copyBeanList(collect,CardVo.class);
        return R.success(cardVos);
    }

    @Override
    public R queryMyCards() {
        List<CardVo> cardVos = userCardService.getCardsByUserId(SecurityUtils.getUserId());
//        if(cardVos.isEmpty()){
//            return R.success(null);
//        }
        return R.success(cardVos);
    }

    @Override
    public R addMyCard(CardDto cardDto) {
        boolean flag = userCardService.save(new UserCard(SecurityUtils.getUserId(), cardDto.getId()));
        if(flag){
            return queryMyCards();
        }
        return R.error(MyEnums.ADD_FAIL);
    }
}